package com.crm.crm.controller;

import com.crm.crm.service.CrmSelectService;
import com.crm.entity.Partner;
import com.crm.model.JsonMessage;
import com.crm.model.Workers;
import com.crm.security.AuthUserDetail;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user/select")
public class CrmSelectController {
    @Autowired
    private CrmSelectService crmSelectService;

    private JsonMessage message = new JsonMessage();


    @PostMapping("personals")
    @ResponseBody
    public JsonMessage personals(String users,
                                 String partnerType,
                                 @RequestParam(defaultValue = "1") int pageIndex,
                                 @RequestParam(defaultValue = "10") int pageSize) {

        String userId = users;
        PageInfo<Partner> page = crmSelectService.personal(Integer.parseInt(partnerType), userId, pageIndex, pageSize);
        message.setData(page);
        return message;
    }

    @PostMapping("selectAsset")
    @ResponseBody
    public JsonMessage selectAsset(
            Authentication authentication, @RequestParam("partnerType") Integer partnerType, String bizType,
            @RequestParam(value = "cities[]", required = false, defaultValue = "") String[] cities, @RequestParam(defaultValue = "1") int pageIndex,
            @RequestParam(defaultValue = "10") int pageSize) {
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        PageInfo<Partner> page = crmSelectService.findAsset(detail.getUserId(), partnerType, bizType, cities, pageIndex, pageSize);
        message.setData(page);
        return message;
    }

    @PostMapping("selectByPost")
    @ResponseBody
    public JsonMessage selectByPost(
            Authentication authentication, @RequestParam("partnerType") Integer partnerType, String post,
            @RequestParam(value = "cities[]", required = false, defaultValue = "") String[] cities, @RequestParam(defaultValue = "1") int pageIndex,
            @RequestParam(defaultValue = "10") int pageSize) {
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();

        PageInfo<Partner> page = crmSelectService.findByPost(detail.getUserId(), partnerType, post, cities, pageIndex, pageSize);
        message.setData(page);
        return message;
    }

    @PostMapping("findByNameTel")
    @ResponseBody
    public JsonMessage findByNameTel(
            Authentication authentication, Integer partnerType,
            @RequestParam(defaultValue = "1") int pageIndex,
            @RequestParam(defaultValue = "10") int pageSize) {

        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();

        PageInfo<Partner> page = crmSelectService.findByNameTel(partnerType, detail.getUserId(), pageIndex, pageSize);
        message.setData(page);
        return message;
    }

    @PostMapping("findByNameTelHide")
    @ResponseBody
    public JsonMessage findByNameTelHide(
            Authentication authentication, Integer partnerType,
            @RequestParam(defaultValue = "1") int pageIndex,
            @RequestParam(defaultValue = "10") int pageSize) {
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();

        if ("002".equals(detail.getRoleId())) {
            PageInfo<Partner> page = crmSelectService.findByNameTelHide1(detail.getRoleId(), partnerType, pageIndex, pageSize);
            message.setData(page);
        } else {
            PageInfo<Partner> page = crmSelectService.findByNameTelHide(detail.getUserId(), detail.getRoleId(), partnerType, pageIndex, pageSize);
            message.setData(page);

        }

        return message;
    }

    @PostMapping("findByBlurry")
    @ResponseBody
    public JsonMessage findByBlurry(Authentication authentication,
                                    Integer partnerType,
                                    String value, String createId,
                                    @RequestParam(defaultValue = "1") int pageIndex,
                                    @RequestParam(defaultValue = "10") int pageSize) {

        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        if ("002".equals(detail.getRoleId())) {
            PageInfo<Partner> page = crmSelectService.findByBlurry01(partnerType, value, pageIndex, pageSize);
            message.setData(page);
        } else {

            PageInfo<Partner> page = crmSelectService.findByBlurry02(partnerType, value, createId, pageIndex, pageSize);
            message.setData(page);
        }
        return message;
    }


    /**
     * @param authentication
     * @param company
     * @param partnerType
     * @param exitType
     * @param orgRemark
     * @param usage
     * @param typeId
     * @param typeName
     * @param cities
     * @param abilityMin
     * @param abilityMax
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @PostMapping("findExitInfo")
    @ResponseBody
    public JsonMessage findExitInfo(Authentication authentication, String depId, String createId,
                                    String company, Integer partnerType, String exitType, String orgRemark,
                                    String usage,
                                    String typeId, String typeName, @RequestParam(value = "cities[]", required = false, defaultValue = "") String[] cities,
                                    Integer abilityMin, Integer abilityMax, @RequestParam(defaultValue = "1") int pageIndex,
                                    @RequestParam(defaultValue = "10") int pageSize) {
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        int min = 0, max = 0;
        PageInfo<Partner> page;
        String userId = "";
        if ("002".equals(detail.getRoleId())) {
            page = crmSelectService.findExitInfo1(depId, createId, company, partnerType, exitType, orgRemark, usage, typeId, typeName, cities, min, max, pageIndex, pageSize);

        } else {
            page = crmSelectService.findExitInfo(userId, detail.getUserId(), company, partnerType, exitType, orgRemark, usage, typeId, typeName, cities, min, max, pageIndex, pageSize);

        }
        message.setData(page);
        return message;
    }

    /**
     * @param authentication
     * @param company
     * @param partnerType
     * @param orgType
     * @param bizListId
     * @param coopListId
     * @param cities
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @PostMapping("findPeerInfo")
    @ResponseBody
    public JsonMessage findPeerInfo(Authentication authentication, String depId, String createId,
                                    String company, Integer partnerType, String orgType,
                                    String bizListId, String coopListId,
                                    @RequestParam(value = "cities[]", required = false, defaultValue = "") String[] cities,
                                    @RequestParam(defaultValue = "1") int pageIndex,
                                    @RequestParam(defaultValue = "10") int pageSize) {
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        PageInfo<Partner> page;
        String userId = "";
        if ("002".equals(detail.getRoleId())) {
            page = crmSelectService.findPeerInfo1(depId, createId, company, partnerType, orgType, bizListId, coopListId, cities, pageIndex, pageSize);

        } else {
            page = crmSelectService.findPeerInfo(userId, detail.getUserId(), company, partnerType, orgType, bizListId, coopListId, cities, pageIndex, pageSize);

        }
        message.setData(page);
        return message;
    }

    @PostMapping("findJusticeInfo")
    @ResponseBody
    public JsonMessage findJusticeInfo(Authentication authentication, String depId, String createId,
                                       Integer partnerType, String company,
                                       String post, @RequestParam(value = "cities[]", defaultValue = "") String[] cities,
                                       @RequestParam(defaultValue = "1") int pageIndex,
                                       @RequestParam(defaultValue = "10") int pageSize) {
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        PageInfo<Partner> page;
        String userId = "";
        if ("002".equals(detail.getRoleId())) {
            page = crmSelectService.findJusticeInfo1(depId, createId, partnerType, company, post, cities, pageIndex, pageSize);

        } else {
            page = crmSelectService.findJusticeInfo(userId, detail.getUserId(), partnerType, company, post, cities, pageIndex, pageSize);

        }
        message.setData(page);
        return message;
    }


    @PostMapping("findOrgInfo")
    @ResponseBody
    public JsonMessage findOrgInfo(Authentication authentication, String depId, String createId,
                                   Integer partnerType, String company,
                                   String post, @RequestParam(value = "cities[]", required = false, defaultValue = "") String[] cities,
                                   @RequestParam(defaultValue = "1") int pageIndex,
                                   @RequestParam(defaultValue = "10") int pageSize) {
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        PageInfo<Partner> page;
        String userId = "";
        if ("002".equals(detail.getRoleId())) {
            page = crmSelectService.findOrgInfo1(depId, createId, partnerType, company, post, cities, pageIndex, pageSize);

        } else {
            page = crmSelectService.findOrgInfo(userId, detail.getUserId(), partnerType, company, post, cities, pageIndex, pageSize);

        }
        message.setData(page);
        return message;
    }

    @PostMapping("findById")
    @ResponseBody
    public JsonMessage findById(Authentication authentication,
                                String partner_id, @RequestParam(defaultValue = "1") int pageIndex,
                                @RequestParam(defaultValue = "10") int pageSize) {

        PageInfo<Partner> page = crmSelectService.findById(partner_id, pageIndex, pageSize);
        message.setData(page);
        return message;
    }

    /**
     * 根据手机号查询
     *
     * @param authentication
     * @param telephone
     * @param create_user_id
     * @return
     */
    @PostMapping("matchNumber")
    @ResponseBody
    public JsonMessage matchNumber(Authentication authentication, String telephone, String create_user_id) {

        Partner partner = crmSelectService.matchNumber(telephone, create_user_id);
        if (partner == null) {
            partner = crmSelectService.telephoneNumber(telephone, create_user_id);
        }
        message.setData(partner);
        return message;
    }

    /**
     * 根据微信查询
     *
     * @param authentication
     * @param weixin
     * @param create_user_id
     * @return
     */
    @PostMapping("weixinNumber")
    @ResponseBody
    public JsonMessage weixinNumber(Authentication authentication, String weixin, String create_user_id) {
        Partner partner = crmSelectService.weixinNumber(weixin, create_user_id);
        message.setData(partner);
        return message;
    }

    /**
     * 根据邮箱查所有
     *
     * @param authentication
     * @param email
     * @param create_user_id
     * @return
     */
    @PostMapping("emailNumber")
    @ResponseBody
    public JsonMessage emailNumber(Authentication authentication, String email, String create_user_id) {

        Partner partner = crmSelectService.emailNumber(email, create_user_id);
        message.setData(partner);
        return message;
    }


    @PostMapping("countCreateId")
    @ResponseBody
    public JsonMessage countCreateId(Authentication authentication) {
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        message.setData(crmSelectService.countCreateId(detail.getUserId()));

        return message;
    }

    @PostMapping("workes")
    @ResponseBody
    public JsonMessage workes() {
        JsonMessage message = new JsonMessage();
        List<Workers> workersList = crmSelectService.workers();
        List<String> depNames = crmSelectService.depName();
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < depNames.size(); i++) {
            List<String> trueName = new ArrayList<>();
            for (int j = 0; j < workersList.size(); j++) {
                if (workersList.get(j).getDepName().equals(depNames.get(i))) {
                    trueName.add(workersList.get(j).getTrueName());
                }
            }
            map.put(depNames.get(i), trueName);
        }
        message.setData(map);
        return message;
    }

}
